﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersistentObject.Common.Criteria
{
    /// <summary>
    /// Represent a decorator on the operand
    /// </summary>
    public class OperandDecorator : Operand
    {
        /// <summary>
        /// The constructor of operand decorator with "{0}" as the default pattern
        /// </summary>
        /// <param name="value">Indicate the real value</param>
        public OperandDecorator(Operand value)
            :this(value, "{0}")
        {
        }

        /// <summary>
        /// The constructor of operand decorator
        /// </summary>
        /// <param name="value">Indicate the real value</param>
        /// <param name="pattern">Indicate the format pattern which replaces {0} by the value</param>
        public OperandDecorator(Operand value, string pattern)
        {
            Pattern = pattern;
            Value = value;

            OperandDecorator od = this;
            while(od != null)
            {
                if(Value is OperandDecorator)
                {
                    od = Value as OperandDecorator;
                }
                else
                {
                    od = null;
                    if (value is OperandValue)
                    {
                        InnerOperandValue = value as OperandValue;
                    }
                    else if(value is OperandProperty)
                    {
                        InnerOperandProperty = value as OperandProperty;
                    }
                }
            }


        }

        public Operand Value { get; private set; }

        /// <summary>
        /// Indicate the format pattern which replaces {0} by the value
        /// </summary>
        public string Pattern { get; private set; }

        public OperandProperty InnerOperandProperty { get; private set; }

        public OperandValue InnerOperandValue { get; private set; }
    }
}
